// ignore_for_file: argument_type_not_assignable_to_error_handler, unrelated_type_equality_checks, prefer_interpolation_to_compose_strings

import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/alarm_util.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/func_utils.dart';
import 'alarm_detail_controller.dart';

class AlarmDetailPage extends GetWidget<AlarmDetailController> {
  AlarmDetailPage({
    Key? key,
  }) : super(key: key);

  @override
  AlarmDetailController get controller => Get.put<AlarmDetailController>(
        AlarmDetailController(),
        tag: Get.parameters['id'],
      );

  final _tagContent = '${ResKey.alarmLevel.tr}: ';

  @override
  Widget build(BuildContext context) {
    Log.d('AlarmUtil.fromModule ===' + AlarmUtil.fromModule.toString());

    return GetBuilder<AlarmDetailController>(
      tag: Get.parameters['id'],
      init: controller,
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            title: Text(ResKey.alarmDetail.tr),
            leading: GestureDetector(
              onTap: () => Get.back(),
              child: Icon(
                Platform.isAndroid ? Icons.arrow_back : Icons.arrow_back_ios,
                color: Colour.black,
              ),
            ),
            actions: <Widget>[
              AlarmUtil.fromModule == AlarmFromWhereType.device.value
                  ? Container()
                  : GestureDetector(
                      onTap: () {
                        controller.getSubDevicesIdList();
                      },
                      child: Padding(
                        padding: EdgeInsets.only(right: 16.w),
                        child: Image.asset(
                          ImageAssets.goDevice,
                          width: 20.w,
                          height: 20.h,
                        ),
                      ),
                    ),
            ],
          ),
          body: GestureDetector(
            onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
            child: _listBody(),
          ),
        );
      },
    );
  }

  Widget _detailInfoWidget() {
    return Container(
      margin: EdgeInsets.only(top: 10, bottom: 10, left: 16.w, right: 16.w),
      padding: EdgeInsets.all(16.w),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      child: Column(
        children: [
          /// 标题
          Row(
            textBaseline: TextBaseline.ideographic,
            children: [
              Visibility(
                visible: controller.model.level != null,
                child: Container(
                  height: 30,
                  margin: const EdgeInsets.only(right: 4, bottom: 0),
                  child: Image.asset(
                    FuncUtils().judgeAlarmIcon(
                      controller.model.level,
                    ),
                    width: 24.w,
                    height: 24.w,
                  ),
                ),
              ),
              Expanded(
                child: Text(
                  controller.model.name ?? '',
                  style: TextStyle(
                    color: Colour.mainTextColor,
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w500,
                  ),
                  overflow: TextOverflow.clip,
                ),
              ),
            ],
          ),
          Gaps.vGap4,

          /// 内容
          ...controller.model.detailInfoLists.map(
            (e) {
              return Container(
                margin: const EdgeInsets.only(top: 4),
                child: Row(
                  children: [
                    SizedBox(
                        width: 98.w,
                        child: Text(e.label, style: TextStyles.lf14)),
                    _tagContent.contains(e.label.toString())
                        ? _statusWidget(e.value)
                        : Expanded(
                            child: Text(
                              e.value,
                              style: ytTextStyle(
                                  height: 1.5,
                                  fontSize: 14,
                                  color: Colour.mainTextColor),
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                  ],
                ),
              );
            },
          ),
        ],
      ),
    );
  }

  Widget _statusWidget(String? level) {
    Color color;

    if (level == ResKey.error.tr) {
      color = const Color(0xFFFA3F3F);
    } else if (level == ResKey.alarmImportant.tr) {
      color = const Color(0xFFF79631);
    } else if (level == ResKey.warn.tr) {
      color = const Color(0xFFFAC958);
    } else if (level == ResKey.info.tr) {
      color = const Color(0xFF0F6EFF);
    } else {
      color = Colour.white;
    }

    return Container(
      // width: 32.w,
      height: 20,
      decoration: BoxDecoration(
        color: color.withOpacity(0.1),
        borderRadius: const BorderRadius.all(
          Radius.circular(4),
        ),
      ),
      child: Padding(
        padding: const EdgeInsets.only(left: 4, right: 4, top: 4),
        child: Center(
          child: Text(
            level!,
            textAlign: TextAlign.center,
            style: TextStyles.styleWith(
              12.sp,
              color: color,
            ),
          ),
        ),
      ),
    );
  }

  Widget _alarmReasonWidget() {
    return Container(
      margin: EdgeInsets.only(bottom: 10.h, left: 16.w, right: 16.w),
      padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 12, bottom: 16),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      child: Column(
        children: [
          Container(
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.only(bottom: 5.w),
            child: Text(
              ResKey.alarmReason.tr,
              style: TextStyle(
                color: Colour.mainTextColor,
                fontSize: 16.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
          ...controller.model.alarmReasonList.map(
            (e) => Container(
              alignment: Alignment.centerLeft,
              // margin: EdgeInsets.only(top: 4.w),
              child: Column(
                children: [
                  Text(
                    e.value,
                    style: TextStyle(
                      color: Colour.otherTextColor,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _repairSuggestionWidget() {
    return Container(
      margin: EdgeInsets.only(bottom: 10, left: 16.w, right: 16.w),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 12, bottom: 16),
      child: Column(
        children: [
          Container(
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.only(bottom: 5.w),
            child: Text(
              ResKey.repairSuggestion.tr,
              style: TextStyle(
                color: Colour.mainTextColor,
                fontSize: 16.sp,
                fontWeight: FontWeight.w500,
              ),
              overflow: TextOverflow.clip,
            ),
          ),
          ...controller.model.repairSuggestionList.map(
            (e) => Container(
              alignment: Alignment.centerLeft,
              // margin: EdgeInsets.only(top: 4.w),
              child: Text(
                e.value,
                textAlign: TextAlign.start,
                style: TextStyle(
                  color: Colour.otherTextColor,
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _siteAddressWidget() {
    return Container(
      margin: EdgeInsets.only(bottom: 10, left: 16.w, right: 16.w),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 12, bottom: 16),
      child: Column(
        children: [
          ...controller.model.siteAddressList.map(
            (e) => Container(
              // margin: EdgeInsets.only(top: 4.w),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Container(
                    // width: 75.w,
                    // color: Colors.red,

                    child: Text(
                      e.label,
                      style: TextStyle(
                        color: Colour.mainTextColor,
                        fontSize: 16.sp,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.only(left: 40, top: 2),
                      // color: Colors.green,
                      child: Text(
                        e.value,
                        textAlign: TextAlign.end,
                        style: TextStyle(
                          color: Colour.lightTextColor,
                          fontSize: 14.sp,
                        ),
                        overflow: TextOverflow.clip,
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _listBody() {
    return ListView.builder(
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) {
        switch (index) {
          case 0:
            return _detailInfoWidget();

          case 1:
            return _alarmReasonWidget();

          case 2:
            return _repairSuggestionWidget();

          case 3:
            return _siteAddressWidget();
        }
      },
      itemCount: 4,
    );
  }
}
